package com.xcc.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xcc.common.constant.SystemConstant;
import com.xcc.common.page.PageParam;
import com.xcc.common.result.ResultBody;
import com.xcc.common.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcc.common.entry.User;
import java.util.List;

import com.xcc.common.mapper.UserMapper;
import com.xcc.common.service.UserService;
/**
   *@name: frosty
   * @description :
   *@date: 2019/11/8
   */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public int updateBatch(List<User> list) {
        return baseMapper.updateBatch(list);
    }
    @Override
    public int batchInsert(List<User> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public ResultBody listByPage(Map<String,Object> map) {
        PageParam page=new PageParam(map);
         User query = page.mapToObj(User.class);
           page.setRecords(userMapper.listByPage(page,query));
        return  ResultBody.success(page);
    }

    @Override
    public ResultBody login(String username, String password) {
        if(StringUtils.isBlank(username)||StringUtils.isBlank(password)){
            return  ResultBody.error("用户名或密码不能为空");
        }


         QueryWrapper queryWrapper=new QueryWrapper();
         queryWrapper.eq("user_name",username);
         User user = userMapper.selectOne(queryWrapper);
         if(user!=null){
             if(password.equals(user.getPassword())){
                  String token = IdWorker.getIdStr();
                  redisUtil.lSet(user.getId(),token);
                  List<Object> objects = redisUtil.lGet(user.getId(), 0, -1);
                      if(objects!=null&&objects.size()>2){
                           //保留的list
                           List list1=  objects.subList(objects.size()-2,objects.size());
                           List list2=objects.subList(0,objects.size()-2);
                             String [] delToken=new String[list2.size()];
                          for (int i=0;i<list2.size();i++) {
                                  delToken[i]=(String)list2.get(i);
                          }
                           redisUtil.del(user.getId());
                           boolean b = redisUtil.lSet(user.getId(), list1);
                          redisUtil.del(delToken);
                      }


                 redisUtil.set(token,user,SystemConstant.TOKEN_ACCENT_TIME);
                  Map<String,Object> t_m=new HashMap<>();
                 t_m.put("token",token);
                 return ResultBody.success(t_m);
             }
         }

        return ResultBody.error("用户名或密码错误!");
    }
}
